package com.kaifamiao.demo.d0815;

import java.util.*;

public class Demo03 {

    public static void main(String[] args) {
        TreeMap treeMap = new TreeMap();

        treeMap.put("a", 1);
        treeMap.put("c", 3);
        treeMap.put("e", 5);
        treeMap.put("b", 2);

        /*
            迭代
              entrySet key-value 集合
              keySet  map 集合中所有键(key)的集合
              values  map 集合中所有值的集合
         */
        // values
//        Collection collection = treeMap.values();
//
//        Iterator iterator =  collection.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//        for (Object obj : collection){
//            System.out.println(obj);
//        }

        // keySet
//        Set set = treeMap.keySet();
//        Iterator iterator =  set.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//        for (Object key : set){
//            System.out.println(key + " = " + treeMap.get(key));
//        }

        // entrySet
        Set set = treeMap.entrySet();
        for (Object obj : set){
            if (obj instanceof Map.Entry entry){
                System.out.println(entry.getKey() + " = "+ entry.getValue());
            }
        }
    }
}
